<?php

namespace app\home\controller;

use think\Controller;

use think\Input;

use think\Db;

use think\Request;

use think\Session;

class Index extends Controller{

	public function index(){  
		
		$user_id = input('get.');
          if($user_id){
			$this->assign('user_id',$user_id['user_id']);
		  }else{
			$this->assign('user_id',0);
		  }
		 
		//查询首页轮播图
		$slider = db('ad')->where('type_id=10')->select();
		
		$this->assign('slider',$slider);
		
		//查询推荐商品  
		$goods = db('aricle')->where("iszd=1")->limit(9)->select();

		//查询商家
		$shangjia = db('admin')->where('admin_id','neq','1')->limit(9)->select();
		
		$this->assign('goods',$goods);
		$this->assign('shangjia',$shangjia);




         if(session('userad')){
			 $this->assign('userad',0);
		 }else{
			$this->assign('userad',1);
		 }

		//  $this->assign('userad',1);
		// $jiangpinInfo=db('jiangpinInfo')->where('ORDER BY RAND()  LIMIT 10')->select();
		// $this->db('jiangpinInfo',$jiangpinInfo);
		return view();}
	
	public function category(){
   	   
	   	$data = db('category')->select();
	   	
	   	 $tree =  $this->get_attr($data,0);
	   	 $this->assign('data',$tree);

	   	return view();
    }
	
    public function get_attr($a,$pid){  

	    $tree = array();                                //每次都声明一个新数组用来放子元素  

	    foreach($a as $v){  

	        if($v['parentid'] == $pid){                      //匹配子记录  

	            $v['children'] = $this->get_attr($a,$v['id']); //递归获取子记录  

	       
	            $tree[] = $v;                           //将记录存入新数组  

	        }  

	    }  

	    return $tree;                                  //返回新数组  

	} 
	
	
	//我的页面
	public function member(){
		return $this->fetch();
	}
	
	//product
	public function product(){
		return $this->fetch();
	}

	//shopcar
	public function shoppingCar(){
		return $this->fetch();
	}

	//登录页面
	public function enter(){
		return $this->fetch();
	}
	
	//商铺页
	public function shangpu(){
		return view();
	}
	
	//店铺列表页
	public function shoplist(){
		return view();
	}
	
	//渲染出发布页
	public function fabu(){
		
		return view();
	}
	
	//将发布页的信息添加到数据库
	public function fabuy(){
		
		$data = input('post.');//接收数据
		
		$data['Otime'] = date('Y-m-d',time());
				
        $fabu =  db('fabu')->insert($data);//将数据传入数据库中
        
        
	}
	


 

 	
   

	//资讯页
	public function message(){
		//从数据库中读取数据
    	 $data=Db::table('clt_fabu')->select();
       //分配数据给页面
      	 $this->assign('data',$data);
       //加载页面
			
		return view();//渲染页面
	}
	

		

	
	
	
	
	
	



	
	//付款页面
	public function Payment(){
		return view();
	}
	


	
	//商铺页面
	public function shopym(){
		return view();
	}
	
	//更多页面
	public function moreshop(){
		return view();
	}
	

	public function zhuanpan(){
		
		$data =	db('jiangpin')->select();
		
		//查询轮播图
		$slider = db('ad')->where('type_id=11')->select();
		
		$this->assign('slider',$slider);
		
	    //查询抽奖机会
	    //获取当前是时间
	     //查询是否有当天的抽奖记录
	    $time = date('Y-m-d',time());

	    $user = session('userad');
	    $map['user_id'] = $user['user_id'];
        $map['adddate'] =['gt',$time];
         $re = db('jiangpininfo')->where($map)->select();
         if($re){
         	
         	$this->assign('num',0);
         	
         }else{
         	$this->assign('num',1);
         }
		
		
		$this->assign('data',json_encode($data));

			return view();
			
	}
	
	
	
	//加入购物车的事件
	public function tianjia(){
		
		db('user')->insertAll($list);//向数据表中添加多条数据
		
		//查询数据表里的内容
		
		$shop = db('')->where('')->select();
		
		$this->assign('shop',$shop);
		
		
		$this->assign('data',json_encode($data));

			return view();
		
	}
	
	//添加中奖详细信息表	
	public function addjiegou(){
	  
		if(request()->isPost())
		{
			$data = input('post.');
			
			//中奖人
			$user = session('userad');
		    $zdata =	$data['jiegou'];
			$zong['good_id'] = $zdata['good_id'];
			$zong['name'] = $zdata['name'];
			$zong['admin_id'] = $zdata['user_id'];
			$zong['user_id'] = $user['user_id'];
			$zong['adddate'] = date('Y-m-d H:i:s',time());
			$zong['phone'] = $user['mobile'];
		$re =	db('jiangpininfo')->insert($zong);
			if($re){
					return 1;
			}else{
			  return 0;
				
			}
        
    


		}
			
		return view();

    }	
	

	//商品详情页面
	public function detail($id=''){
		
		$data = db('aricle')->find($id);
	   
	    $this->assign('data',$data);
	    
	    $this->assign('good',json_encode($data));
	   
	    if($data['shuxing'] && $data['value']){
	    	
	    	$shuxing = json_decode($data['shuxing']);
	    	 	 
	    	$sv = json_decode($data['value']);
	    	
	    	$aaa = [];
	    	
	    	for($i=0;$i<count($shuxing);$i++){
	    	          $aaa[$i]['shuxing'] = $shuxing[$i];
	    	 	    $aaa[$i]['child'] = array();
	    	 	foreach($sv  as $k=>$v){
	    	 		
	    	 		$cc = explode(':',$k); 
	    	 		if(!in_array($cc[$i], $aaa[$i]['child'])){
	    	 			array_push($aaa[$i]['child'],$cc[$i]);
	    	 			
	    	 		}
	    	 		
	    	 	}
	    	 		
	    	}
	    	 
	    	$this->assign('shuxing',$aaa);
    	
    	}
    	

  
   
   
	    if($data['value']){
	    	$this->assign('value',$data['value']);
		}
		
		$slider = json_decode($data['images']);
		$this->assign('slider',$slider);
		
        return view();
	}
	


	
	public function ajaxsubmit(){
		
                  
        $data = input('post.');//接收数据

        $car =  db('single')->insert($data);//将数据传入数据库中

  			if($car){//如果传进去了
  	
  				return 1;//返回1
  	
  			}else{//否则
  		
  				return 0;//返回0
 		 }
		
    }



	//登录页面
	public function mine(){
	
		if(request()->isPost()){
			$data = input('post.');
	
			$data['password'] = md5($data['password']);
			$map['nickname'] = $data['nickname'];
			$user = db('users')->where($map)->find();
			if($user){
				if($user = db('users')->where($data)->find()){
					  
					  session('userad',$user);
					  
				  return ['code'=>1,'msg'=>'登陆成功!','url'=>url('index')];
				}else{
					return array('code'=>0,'msg'=>"密码错误");
				}
				
			}else{
				
				return array('code'=>0,'msg'=>"用户名错误");
			}
			
		}
		
		if( session('userad')){
			return view('member');	
		}else{
			return view('enter');	
		}
	




	}
	
	//普通注册	
	public function zhuche($user_id=0){
		

			if(request()->isPost()){
			
				$data = input('post.');
				
				$map['nickname'] = $data['nickname'];
				
				$user = db('users')->where($map)->find();
			
				if($user){
					
					return ['code'=>0,'msg'=>'用户名已经存在!'];
					
				}else{

					$data['password'] = md5($data['password']);
					$data['reg_time'] = time();
					$data['statecheck']=0;
					
					$user = db('users')->insert($data);

					if($user){
						 return ['code'=>1,'msg'=>'注册成功!','url'=>url('mine')];
					}else{
						 return ['code'=>0,'msg'=>'网络异常!'];
						
					}
					
				}
			}

		$this->assign('user_id',$user_id);
		return view();

	}



	//三级分销注册
	public  function zhuce1(){
		$data = input('post.');
				
		$map['nickname'] = $data['nickname'];
		
		$user = db('users')->where($map)->find();
	
		if($user){
			
			return ['code'=>0,'msg'=>'用户名已经存在!'];
			
		}else{

			$data['password'] = md5($data['password']);
			$data['reg_time'] = time();
			$data['statecheck']=3;
			
			$user = db('users')->insert($data);

			if($user){
				 return ['code'=>1,'msg'=>'注册成功!','url'=>url('mine')];
			}else{
				 return ['code'=>0,'msg'=>'网络异常!'];
				
			}

		}
		
	}




	//非分销会员页面
	public function fenxiao(){
		
		//从session中获取用户信息
		$user = session('userad');
        
		//如果获取成功则查询用户会员级别
		if(is_null($user)){
			return;
		}else{
			$statecheck = $user['statecheck'];
		};

		//渲染页面，同时把会员级别发送到前端
		return $this->fetch('fenxiao',[
			'statecheck'=>$statecheck,
		]);
	}



	//已分销会员页面
	public function yifenxiao(){
		//从session中获取用户信息
		$user = session('userad');
        
		//如果获取成功则查询用户会员级别
		if(is_null($user)){
			return;
		}else{
			
			//分销商推荐人判断
			if($user['parentid']==0){
				
				$this->assign("parentid","管理员");
				
			}else{
				
				$result = db('users')->where("user_id",$user['parentid'])->value('nickname');

				if($result){
					$this->assign("parentid",$result);	
				}else{

				}
			}
		};

		//分销中佣金
		$total = intval($user['sendmoney']) + intval($user['money']);
		
		//渲染页面，同时把会员级别发送到前端
		return $this->fetch('yifenxiao',[
			'statecheck'=>$user['statecheck'],
			'nickname'=>$user['nickname'], 
			'sendmoney'=>$user['sendmoney'],
			'money'=>$user['money'],
			'user_id'=>$user['user_id'], 
			'total'=>$total
		]);
	}


	//申请分销，提交真实身份
	public function sub(){

		if(request()->isPost()){
			
			$data = input('post.');

			$a = Session("userad");

			if($a["name"]){
					
				 return ['code'=>0,'msg'=>'别闹，亲，你已经提交过实名信息了'];
				
			}else{
			
				$a['name'] = $data['name'];
				$a['birthday'] = $data['birthday'];
				$a['mobile'] = $data['mobile'];
                $a['statecheck'] = 1;
				$user = db('users')->update($a);
				
				if($user){
                    //更新session
				 	Session("userad",$a);
				 	return ['code'=>1,'msg'=>'提交数据成功!'];
				}else{
				 	return ['code'=>0,'msg'=>'提交数据失败!'];
			 }
			
			}
		}
	}
	
	
	//退出登录
	public function layout(){
		
		session('userad',null);

		$this->redirect('mine');
		
	}



  //大转盘积分兑换
  public function deljf(){
  	$user = session('userad');
  	$map['user_id'] = $user['user_id'];
  	$data =  db('users')->where($map)->find();
  	if(floatval($data['jifen']) >= 1){
  		
  	
  	$data['jifen'] = floatval($data['jifen'])-1;
  $res =	db('users')->update($data);
  if($res) {
  	  //更新session
  	  session('userad',$data);
 	return json(['code'=>200,'msg'=>'兑换成功']);
  }else{
  	return json(['code'=>402,'msg'=>'网络出错']);
  	
  }
  }else{
  	
  	return json(['code'=>401,'msg'=>'积分不足，无法兑换']);
  }
  	

  }
	
	
	public function jilu(){
		
		
			//查询轮播图
		$slider = db('ad')->where('type_id=11')->select();
		
		$this->assign('slider',$slider);
		
		$user = session('userad');
		
		$data = db('jiangpininfo')->where("user_id=".$user['user_id'])->select();
		
		$this->assign('data',$data);
		$this->assign('count',count($data));
		return view();
	}
	
}

